# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
	# Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem

	# If you want "remember me" functionality, add this before_filter to Application Controller
	before_filter :login_from_cookie

  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_website_session_id'
  
  # This method is used in each of the pages controllers to check whether a user is logged in,
  # and whether a page should be displayed to a user who is not logged in.
  # Usage => access_check("solutions")
  # That will check the access controls around the Solutions home page and sub-pages.
  def access_check(section)
    if logged_in?
      @section_pages = Page.sub_pages(section)
      render 'pages/show'
    else
            
      @section_pages = Array.new
      @page.section.section_pages.each do |page|
        if page.published?
          @section_pages << page
        end
      end
    end
  
    unless logged_in?
      unless @page.published?
        render :file => "#{RAILS_ROOT}/public/404.html", :status => '404 Not Found'
      else
        render 'pages/show'
      end
    end
  end
	
	def admin_required
	  current_user.is_admin? || not_admin
	end
	
	def not_admin
	  flash[:notice] = "You must be an Admin to do that."
	  redirect_to dashboards_path
	end
  
end